#ifndef __BUOLA_CV_COCTREEVIEW_H__
#define __BUOLA_CV_COCTREEVIEW_H__

#include <buola/gui.h>
#include <buola/gui/cmenu.h>
#include <buola/gui/cwindow.h>

#include <buola/gl.h>

#include <buola/app/ceventloop.h>
#include <buola/cv/coctree.h>
#include <buola/mat/cquat.h>

namespace buola { namespace cvi {

class COcTreeView : public gui::CWindow, public CLoopFunction
{
    enum EFlags
    {
        FLAG_NONE=0,
        FLAG_HAVEVBO=(1<<2),
        FLAG_HAVECAMERAINFO=(1<<3),
        FLAG_HAVEUPVECTOR=(1<<4),
        FLAG_HAVELOOKATPOS=(1<<5)
    };

    DECLARE_FRIEND_ENUM_BITOPS(EFlags)

public:
    COcTreeView();

    void SetOcTree(COcTree<float> &&pTree);
    
    void SetCameraPos(const mat::CVec3d &pPos);
    void SetUpVector(const mat::CVec3d &pUp);
    void SetLookAtPos(const mat::CVec3d &pCenter);

private:
    void DrawPoints();
    void GenerateCameraInfo();
    void GenerateVBOs();

    virtual void OnExpose(gui::CExposeEvent*);
    virtual void OnButtonDown(const gui::CMouseEvent &pE);
    virtual void OnButtonUp(const gui::CMouseEvent &pE);
    virtual void OnMouseMove(const gui::CMouseEvent &pE);

private:
    COcTree<float> mTree;
    
    double mZoom;
    mat::CQuat_d mRotation;
    mat::CQuat_d mDragRotation;
    bool mDragging;
    CPoint_d mDragPoint;

    mat::CVec3d mCameraPos;
    mat::CVec3d mUpVector;
    mat::CVec3d mRightVector;
    mat::CVec3d mUserUpVector;
    mat::CVec3d mLookAtPos;
    
    EFlags mFlags;

    GLuint mVertexBO,mIndexBO;

    int mNumVertices,mNumIndices;
};

typedef intrusive_ptr<COcTreeView> POcTreeView;

/*namespace cvi*/ } /*namespace buola*/ }

#endif
